﻿Shader "ImageOuterOutline"
{
    Properties
    {
        _MainTex("Sprite Texture", 2D) = "white" {}
        _TexColor("颜色", Color) = (1,1,1,1)
        _Color("描边颜色", Color) = (1,1,1,1)
        _Range("描边范围", Vector) = (0,0,0,0)
        _Step("Step", Float) = 0.01
        _Size("Size", Float) = 2
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent"}
        LOD 100

        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass 
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            half4 _Color;
            half4 _TexColor;
            half4 _Range;
            half _Step;
            half _Size;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            float4 frag(v2f input) : SV_Target
            {
                float colorAlpha = 0;
                _Size = (int)_Size;

                for (int x = -_Size; x <= _Size; x++)
                {
                    for (int y = -_Size; y <= _Size; y++)
                    {
                        colorAlpha += tex2D(_MainTex, input.uv + float2(x, y) * _Step).a;
                    }
                }

                colorAlpha /= (_Size * 2 + 1) * (_Size * 2 + 1);

                float4 color = tex2D(_MainTex, input.uv);
                color.rgb = lerp(color.rgb * _TexColor.rgb, _Color.rgb, step(_Range.x, colorAlpha) * step(colorAlpha, _Range.y));

                return color;
            }
            ENDCG
        }
    }
}